package com.baizhi.jh.service;

import com.baizhi.jh.dao.AdminDao;
import com.baizhi.jh.entity.Admin;
import com.baizhi.jh.utils.ImageCodeUtil;
import com.baizhi.jh.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class AdminServiceImpl implements AdminService{
    @Autowired
    private AdminDao adminDao;

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void insert(Admin admin){
        adminDao.insert(admin);
        System.out.println("原始数据：");
        String password=admin.getPassword();
        System.out.println(password);
        //对传过来的数据进行加工
        //调用工具类  对原始密码加密


        //对原始密码加随机盐
        String salt=Md5Utils.getSalt(6);
        System.out.println("salt:"+salt);
        System.out.println("加工后的数据：");
        String saltPwd=salt+password+salt;
        System.out.println("salt Pwd"+saltPwd);

        String md5Code=Md5Utils.getMd5Code(saltPwd);
        System.out.println(md5Code);

        //对穿过来的管理员的数据进行数据加工
        admin.setPassword(md5Code);
        admin.setSalt(salt);
        adminDao.insert(admin);

    }
    //登录
    public Map<String,String> login(Admin admin,String token){

        Map<String,String> map = new HashMap<String,String>();
        //从redis中获取服务器上的验证码
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object obj = valueOperations.get("CODE_" + token);
        //检查验证码
        if(obj==null){
            map.put("message","验证码过期");
            map.put("state","error");
            return map;
        }
        String serverCode =obj.toString();
        if(!admin.getEnCode().equals(serverCode)){//验证码错误
            map.put("message","验证码11错误");
            map.put("state","error");
            return map;
        }
        //验证码正确，检查账号密码
        Admin user = adminDao.selectByUsername(admin.getUsername());
        System.out.println(user);
        if(user==null){
            map.put("message","账号不存在");
            map.put("state","error");
            return map;
        }else{
            if(user.getStatus()=="0"){
                map.put("message","用户已被冻结");
                map.put("state","error");
                return map;
            }
            //获取输入的密码
            String password = admin.getPassword();
            String salt = user.getSalt();
            //加DB相同的盐
            String md5Code = Md5Utils.getMd5Code(salt + password + salt);
            //对加盐后的密码md5 md5Pwd
            if(md5Code.equals(user.getPassword())){
                map.put("state","success");
                map.put("message",token);
                valueOperations.set("TOKEN_"+token,user);
                return map;
            }else {
                map.put("message","密码错误");
                map.put("state","error");
                return map;
            }
        }
    }
    //获取登录用户信息
    public Admin getUserInfo(String token){
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations valueOperations = redisTemplate.opsForValue();
        return (Admin) valueOperations.get("TOKEN_"+token);
    }
    //退出
    public void logout(String token){
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.delete("TOKEN_"+token);
    }

    @Override
    public int queryTotalCount(int pageSize) {
        return 0;
    }

    @Override
    public List<Admin> queryByPage(int curPage, int pageSize) {
        return null;
    }

    //生成验证码
    public Map<String,String> getImageCodes(){
        Map<String,String> map = new HashMap<>();
        //调用工具类 获取随机验证码字符
        String code = ImageCodeUtil.getSecurityCode();
        //调用工具类 根据随机码 生成图片base64形式
        try {
            String imgBase64 = ImageCodeUtil.careateImgBase64(code);
            //把base图片信息存入map
            map.put("imgCode",imgBase64);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取UUID
        String uuid = UUID.randomUUID().toString();
        //把UUID存入map
        map.put("token",uuid);
        //往redis中存验证码
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("CODE_"+uuid,code,1, TimeUnit.MINUTES);

        return map;
    }
}
